﻿using System;
using System.Collections.Generic;
using System.Linq;
using FoodRaidersWebAPI.domainnew.Article;
using FoodRaidersWebAPI.domainnew.Favorite;
using FoodRaidersWebAPI.domainnew.other;
using FoodRaidersWebAPI.Models;

namespace FoodRaidersWebAPI.Service.First
{
    public class UserService
    {
        private readonly FoodContext context;
        private readonly FoodArticleService foodArticleService;
        private readonly MerchantArticleService merchantArticleService;
        private readonly RaiderArticleService raiderArticleService;

        public UserService(FoodContext context)
        {
            this.context = context;
            foodArticleService = new FoodArticleService(context);
            merchantArticleService = new MerchantArticleService(context);
            raiderArticleService = new RaiderArticleService(context);
        }

        //id查询
        public User findByID(int id)
        {
            var user = context.Users.FirstOrDefault(u => u.UserID == id);
            return user;
        }
        
        //fid美食分享加入uid的收藏美食分享列表
        public void addFavoriteFood(int uid, int fid)
        {
            var ffa = new FavoriteFoodArticle()
            {
                DateTime = DateTime.Now, FoodArticleID = fid, UserID = uid
            };
            context.Add(ffa);
            context.SaveChanges();
        }
        //fid美食分享从uid的收藏美食分享列表移除
        public void subFavoriteFood(int uid, int fid)
        {
            var favoriteFoodArticle = context.FavoriteFoodArticles.FirstOrDefault(f => f.UserID == uid && f.FoodArticleID == fid);
            context.FavoriteFoodArticles.Remove(favoriteFoodArticle);
            context.SaveChanges();
        }
        
        //mid商家分享加入uid的收藏商家分享列表
        public void addFavoriteMerchant(int uid, int mid)
        {
            var ffa = new FavoriteMerchantArticle()
            {
                DateTime = DateTime.Now, MerchantArticleID = mid, UserID = uid
            };
            context.Add(ffa);
            context.SaveChanges();
        }
        //mid商家分享从uid的收藏商家分享移除
        public void subFavoriteMerchant(int uid, int mid)
        {
            var favoriteMerchantArticle = context.FavoriteMerchantArticles.FirstOrDefault(m => m.UserID == uid && m.MerchantArticleID == mid);
            context.FavoriteMerchantArticles.Remove(favoriteMerchantArticle);
            context.SaveChanges();
        }
        
        //rid攻略分享加入uid的收藏攻略分享列表
        public void addFavoriteRaider(int uid, int rid)
        {
            var ffa = new FavoriteRaiderArticle()
            {
                DateTime = DateTime.Now, RaiderArticleID = rid, UserID = uid
            };
            context.Add(ffa);
            context.SaveChanges();
        }
        //rid攻略分享从uid的收藏攻略分享列表移除
        public void subFavoriteRaider(int uid, int rid)
        {
            var favoriteRaiderArticle = context.FavoriteRaiderArticles.FirstOrDefault(r => r.UserID == uid && r.RaiderArticleID == rid);
            context.FavoriteRaiderArticles.Remove(favoriteRaiderArticle);
            context.SaveChanges();
        }
        //增加User
        public void addUser(User user)
        {
            context.Users.Add(user);
            context.SaveChanges();
        }
        
        //返回用户发布的<美食分享>的线性表（不加载评论）
        public List<FoodArticle> FoodArticles(int id)
        {
            var list = new List<FoodArticle>();
            var query = context.FoodArticles.Where(f => f.UserID == id).Select(f => f.FoodArticleID).ToList();
            query.ForEach(q =>
            {
                list.Add(foodArticleService.findByIDNoCommentLoad(q));
            });
            return list;
        }
        //返回用户发布的<商家分享>的线性表（不加载评论）
        public List<MerchantArticle> MerchantArticles(int id)
        {
            var list = new List<MerchantArticle>();
            var query = context.MerchantArticles.Where(f => f.UserID == id).Select(f => f.MerchantArticleID).ToList();
            query.ForEach(q =>
            {
                list.Add(merchantArticleService.findByIDNoCommentLoad(q));
            });
            return list;
        }
        //返回用户发布的<攻略分享>的线性表（不加载评论）
        public List<RaiderArticle> RaiderArticles(int id)
        {
            var list = new List<RaiderArticle>();
            var query = context.RaiderArticles.Where(f => f.UserID == id).Select(f => f.RaiderArticleID).ToList();
            query.ForEach(q =>
            {
                list.Add(raiderArticleService.findByIDFullNoCommentLoad(q));
            });
            return list;
        }
        //用户收藏的<美食分享收藏>的线性表（不加载评论）
        public List<FavoriteFoodArticle> FavoriteFoodArticles(int id)
        {
            var query = context.FavoriteFoodArticles.Where(f => f.UserID == id).ToList();
            var list = new List<FavoriteFoodArticle>();
            query.ForEach(q =>
            {
                q.FoodArticle = foodArticleService.findByIDNoCommentLoad(q.FoodArticleID);
                list.Add(q);
            });
            return list;
        }
        //用户收藏的<商家分享收藏>的线性表（不加载评论）
        public List<FavoriteMerchantArticle> FavoriteMerchantArticles(int id)
        {
            var query = context.FavoriteMerchantArticles.Where(f => f.UserID == id).ToList();
            var list = new List<FavoriteMerchantArticle>();
            query.ForEach(q =>
            {
                q.MerchantArticle = merchantArticleService.findByIDNoCommentLoad(q.MerchantArticleID);
                list.Add(q);
            });
            return list;
        }
        //用户收藏的<攻略分享收藏>的线性表（不加载评论）
        public List<FavoriteRaiderArticle> FavoriteRaiderArticles(int id)
        {
            var query = context.FavoriteRaiderArticles.Where(f => f.UserID == id).ToList();
            var list = new List<FavoriteRaiderArticle>();
            query.ForEach(q =>
            {
                q.RaiderArticle = raiderArticleService.findByIDFullNoCommentLoad(q.RaiderArticleID);
                list.Add(q);
            });
            return list;
        }
    }
}